﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//MicroDesigner
using Jc.MicroDesigner.Objects;
using Jc.MicroDesigner.Management;

namespace Jc.MicroDesigner.ToolWindows
{
    /// <summary>
    /// Форма выбора пина на схеме
    /// </summary>
    public partial class PINSelectForm : Form
    {
        ObjectManager _Manager = null;
        PIN _Selected = null;

        /// <summary>
        /// Создать диалог выбора пина
        /// </summary>
        public PINSelectForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Выбранный пин
        /// </summary>
        public PIN SelectedPIN
        {
            get
            {
                return _Selected;
            }
        }

        /// <summary>
        /// Показать диалог выбора пина
        /// </summary>
        /// <param name="manager">Схема</param>
        /// <returns>Выбранный пин</returns>
        public DialogResult ShowDialog(ObjectManager manager)
        {
            _Manager = manager;
            if (manager != null)
            {
                foreach (BaseElement item in manager.Elements)
                {
                    if ((item is CircuitElement) && !(item is Wire))
                    {
                        CircuitElement ce = item as CircuitElement;
                        TreeNode node = new TreeNode(ce.Name);
                        //сохраняем данные в элемент
                        node.Tag = ce;
                        //добавить иконку
                        node.ImageIndex = 0;
                        node.SelectedImageIndex = 0;
                        foreach (PIN pin in ce.PINs)
                        {
                            TreeNode pnode = new TreeNode(ce.Name + " :: " + pin.Name + " : " + pin.TypeToString());
                            //сохраняем данные в элемент
                            pnode.Tag = pin;
                            //добавить иконку
                            pnode.ImageIndex = 1;
                            pnode.SelectedImageIndex = 1;
                            node.Nodes.Add(pnode);
                        }
                        ElementsTree.Nodes.Add(node);
                    }
                }
            }
            return ShowDialog();
        }

        private void ElementsTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            _Selected = null;
            SelectLabel.Text = "Не выбран";
            if (e.Node.Level == 1)
            {
                _Selected = (PIN)e.Node.Tag;
                SelectLabel.Text = e.Node.Text;
            }
        }

        private void ClearBtn_Click(object sender, EventArgs e)
        {
            _Selected = null;
            SelectLabel.Text = "Не выбран";
        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}